package com.example.android1.osstruts.logs.rxjava.rxjava;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;

/**
 * Created by Mjj on 2017/10/6.
 */

public class RxjavaCreateDemo {

    //第一步：创建被观察者：Observable.create
    Observable observable = Observable.create(new Observable.OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> subscriber) {//Subscriber观察者
            subscriber.onNext("Hello");
            subscriber.onNext("Imooc");
            subscriber.onCompleted();
        }
    });
    //②.通过just方法来创建被观察者
//    Observable observableJust = Observable.just("hello", "Imooc");
//    //③.通过from方法来创建被观察者
//    String[] parameters = {"hello", "Imooc"};
//    Observable observableFrom = Observable.from(parameters);

    //第二步：创建观察者
    Observer<Object> observer = new Observer<Object>() {
        @Override
        public void onCompleted() {//当不再有新的事件，通过被观察者发出的时候回调的方法。
        }
        @Override
        public void onError(Throwable e) {//处理异常框架
        }
        @Override
        public void onNext(Object s) {//相当于update()
        }
    };
    Subscriber<String> subscriber = new Subscriber<String>() {
        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onNext(String s) {
        }
    };

    public void doRxjava(){
        //第三步：订阅关系建立， 被观察者observable，订阅观察者observer
        observable.subscribe(observer);
        observable.subscribe(subscriber);
    }
}
